package br.com.roteador.component;

import java.io.Serializable;

import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.log4j.Logger;

//@WebListener
public class LocalizacaoPushService implements ServletContextListener {
	@Inject LocalizacaoPushThread thread;
	private static final Logger LOG = Logger.getLogger(LocalizacaoPushService.class);
	
	public static class SomeDummyEvent implements Serializable {
		private static final long serialVersionUID = 5115584665644622390L;
    }
	
	public BeanManager lookUpBeanManager() {
        try {
            InitialContext iniCtx = new InitialContext();
            BeanManager result = (BeanManager) iniCtx.lookup("java:comp/env/BeanManager");
            return result;
        } catch (NamingException e) {
            LOG.error("Could not construct BeanManager.", e);
            return null;
        }
    }

   public void contextInitialized(ServletContextEvent sce) {
	   BeanManager beanManager = lookUpBeanManager();
       if (beanManager != null) {
           beanManager.fireEvent(new SomeDummyEvent());
           LOG.info("beanManager fired SomeDummyEvent.");
       } else {
           LOG.error("beanManager is null.  Cannot fire startup event.");
       }
   }
   
   public void contextDestroyed(ServletContextEvent sce) {}
   
}